import { defineStore } from 'pinia'
import request from '../network/request.js'

export const useUserStore = defineStore('user', {
	state: () => {
		return {
			userInfo: {},  //用户信息
			// 用户钱包收益相关
			userProfit: {
				total_money: 0,  //总收入
				current_money: 0, // 可提现
				total_pay: 0, // 总支出
			},
			isChooseWithdrawAccount: false,  // 是否需要选择提现账户
		}
	},
	getters: {
		userId() {
			return this.userInfo?.id || 0
		},
		openid() {
			return this.userInfo?.wx_openid || 0
		},
		islogin() {
			return this.userInfo?.id > 0 ? true : false
		}
	},
	actions: {
		async getUserInfo() {
			const res = await request.post('/api/user/get_userinfo',{},{load: false})
			if (res.code == 200) {
				const data = res?.data || {}
				for (let key in data) {
					if (data.hasOwnProperty(key)) {
						this.userInfo[key] = data[key]
					}
				}
				uni.setStorageSync('userInfo', this.userInfo)
			}
		},
		// 对比个人账号与商户账号是否一致
		async diffCash() {
			const { user_cert_status, merchant_cert_status } = this.userInfo
			if (user_cert_status == 2 && merchant_cert_status == 2) {
				const res = await Promise.all([
					request.get('/api/user/cert_detail'),
					request.get('/api/merchant_cert/detail')
				])
				const p = res[0].data,
					s = res[1].data
				if (p.bank_no != s.bank_no) {
					this.isChooseWithdrawAccount = true
				}
			}
		}
	}
})